//
//  LSQPushViewController.m
//  Style
//
//  Created by dllo on 15/9/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "LSQPushViewController.h"
#import "LSQPushTableViewCell.h"
#import "LSQpushModel.h"
#import "UMSocial.h"
#define ScreenWidth          [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight         [[UIScreen mainScreen] bounds].size.height
#define ViewWidth(v)                        v.frame.size.width
#define ViewHeight(v)                       v.frame.size.height
@interface LSQPushViewController ()<UIWebViewDelegate>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *arrayUrl;
@property (nonatomic ,strong) UIWebView *webView;
@property (nonatomic ,retain) UIActivityIndicatorView *activityIndicator;
@property (nonatomic, assign) NSInteger temp;
@property (nonatomic, retain) UIView *viewSize;
@end

@implementation LSQPushViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
       self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = NO;
    NSURL *url2 = [NSURL URLWithString:self.url];
    NSString *str2 = [NSString stringWithContentsOfURL:url2 encoding:NSUTF8StringEncoding error:nil];
    self.title = self.name;
   // NSLog(@"str2:%@",str2);
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,- ScreenHeight / 6.67, ScreenWidth, ScreenHeight + ScreenHeight / 6.67 - 44)];
    [self.webView loadHTMLString:str2 baseURL:nil];
        self.webView.delegate = self;
       [self.view addSubview:self.webView];
    self.webView.scrollView.bounces = NO;
    self.temp = 0;

    UIView *viewR = [[UIView alloc] initWithFrame:CGRectMake(ViewWidth(self.view) / 3, ViewHeight(self.view) - 108, ViewWidth(self.view) * 2 / 3, 44)];
    viewR.backgroundColor = [UIColor whiteColor];
    viewR.layer.borderWidth = 0.3;
    [self.view addSubview:viewR];
    
    self.viewSize = [[UIView alloc] initWithFrame:CGRectMake(ViewWidth(self.view) / 3, ViewHeight(self.view) - 108, ViewWidth(self.view) * 2 / 3, 44)];
    self.viewSize.backgroundColor = [UIColor whiteColor];
    self.viewSize.layer.borderWidth = 0.3;
    self.viewSize.alpha = 0;
    [self.view addSubview:self.viewSize];
    
    UIButton *buttonS = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonS.frame = CGRectMake(0, 0, ViewWidth(self.viewSize) / 3, ViewHeight(self.viewSize));
    [buttonS setImage:[UIImage imageNamed:@"S"] forState:UIControlStateNormal];
    [self.viewSize addSubview:buttonS];
    [buttonS addTarget:self action:@selector(changeS) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonM = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonM.frame = CGRectMake(ViewWidth(self.viewSize) / 3, 0, ViewWidth(self.viewSize) / 3, ViewHeight(self.viewSize));
    [buttonM setImage:[UIImage imageNamed:@"M"] forState:UIControlStateNormal];
    [self.viewSize addSubview:buttonM];
    [buttonM addTarget:self action:@selector(changeM) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonB = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonB.frame = CGRectMake(ViewWidth(self.viewSize) / 3 * 2, 0, ViewWidth(self.viewSize) / 3, ViewHeight(self.viewSize));
    [buttonB setImage:[UIImage imageNamed:@"B"] forState:UIControlStateNormal];
    [self.viewSize addSubview:buttonB];
    [buttonB addTarget:self action:@selector(changeB) forControlEvents:UIControlEventTouchUpInside];
    
    /* 分享按钮 */
    UIButton *buttonShare = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonShare.frame = CGRectMake(ViewWidth(viewR) / 2 - 15, ViewHeight(viewR) / 2 - 15, 30, 30);
    [buttonShare setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [viewR addSubview:buttonShare];
    [buttonShare addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];

    UIView *viewL = [[UIView alloc] initWithFrame:CGRectMake(0, ViewHeight(self.view) - 108, ViewWidth(self.view) / 3, 44)];
    viewL.backgroundColor = [UIColor whiteColor];
    viewL.layer.borderWidth = 0.3;
    [self.view addSubview:viewL];
    
    /* 大小按钮 */
    UIButton *buttonSize = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonSize.frame = CGRectMake(ViewWidth(viewL) / 2 - 15, ViewHeight(viewL) / 2 - 15, 30, 30);
    [buttonSize setImage:[UIImage imageNamed:@"size"] forState:UIControlStateNormal];
    [buttonSize addTarget:self action:@selector(buttonSizeClicked:) forControlEvents:UIControlEventTouchUpInside];
    [viewL addSubview:buttonSize];
    
}
//加载完成
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //字体颜色
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
    //页面背景色
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].style.background='#2E2E2E'"];
    //图片自适应
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth= 355;"//缩放系数,调动这个参数可以改变图片的宽度
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "oldwidth = myimg.width;"
     "if (oldwidth >= 300) {"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height;"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    /* 改变字体大小 */
     //  NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'";
      //[webView stringByEvaluatingJavaScriptFromString:str];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    [self.activityIndicator stopAnimating];
}

/*开始加载*/
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    
    self.activityIndicator = [[UIActivityIndicatorView alloc] init];
    self.activityIndicator.frame = CGRectMake(0, 0, 50, 50);
    self.activityIndicator.center = self.view.center;
    [self.view addSubview:_activityIndicator];
    self.activityIndicator.color = [UIColor blackColor];
    [_activityIndicator startAnimating];
}

/*加载错误*/

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    [_activityIndicator stopAnimating];
 
    
    [webView reload];
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (self.temp++ == 0) {
        return YES;
    }
    return NO;
}

/* 改变字体大小按钮 */
- (void)buttonSizeClicked:(UIButton *)button
{
    [UIView animateWithDuration:0.3 animations:^{
        self.viewSize.alpha = 1;
    }];
}

- (void)changeS
{
    //     改变字体大小
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'";
    [self.webView stringByEvaluatingJavaScriptFromString:str];
    [UIView animateWithDuration:0.3 animations:^{
        self.viewSize.alpha = 0;
    }];
}

- (void)changeM
{
    //     改变字体大小
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '125%'";
    [self.webView stringByEvaluatingJavaScriptFromString:str];
    [UIView animateWithDuration:0.3 animations:^{
        self.viewSize.alpha = 0;
    }];
}

- (void)changeB
{
    //     改变字体大小
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '150%'";
    [self.webView stringByEvaluatingJavaScriptFromString:str];
    [UIView animateWithDuration:0.3 animations:^{
        self.viewSize.alpha = 0;
    }];
}

/* 分享 */
- (void)share
{
    //注意：分享到微信好友、微信朋友圈、微信收藏、QQ空间、QQ好友、来往好友、来往朋友圈、易信好友、易信朋友圈、Facebook、Twitter、Instagram等平台需要参考各自的集成方法
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55e26fd6e0f55a666100490e"
                                      shareText:self.url
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession, UMShareToEmail,nil]
                                       delegate:nil];
}



//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//    //计算label高
//    //根据对应的文字求出cell上的label显示的高度
//    NSDictionary *fontDic=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:23],NSFontAttributeName, nil];
//    //根据文字的大小,计算出文本的尺寸
//    //还需要执行一个尺寸(375,0)
//    //第三个参数,计算高度需要根据字体的哪个特征来确定
//    CGRect rect = [[self.arrayUrl [indexPath.row]title] boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
//    //最后把结果作为返回值返回
//    //NSLog(@"%f, %f", rowHeight , rect.size.height);
//    
//    NSDictionary *fontDic1=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
//    CGRect rect1 = [[self.arrayUrl[indexPath.row] content] boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic1 context:nil];
//    
//
//    
//    
//    
//    return (rect.size.height + 40) + rect1.size.height + 15 + 20 ;
//
//
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
